package com.pwt.service;

import com.pwt.common.CustomException;
import com.pwt.common.ResultPage;
import com.pwt.entity.AcceptanceLimit;
import com.pwt.entity.QAcceptanceLimit;
import com.pwt.enu.AcceptanceEnum;
import com.pwt.model.ComboBoxModel;
import com.pwt.repository.AcceptanceLimitRepository;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * AcceptanceLimit service层
 *
 * @author auto generated
 * @date 2021-09-29 09:53:59
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AcceptanceLimitService {

    private AcceptanceLimitRepository acceptanceLimitRepository;

    private JPAQueryFactory jpaQueryFactory;



    public AcceptanceLimitService(AcceptanceLimitRepository acceptanceLimitRepository, JPAQueryFactory jpaQueryFactory) {
        this.acceptanceLimitRepository = acceptanceLimitRepository;
        this.jpaQueryFactory = jpaQueryFactory;
    }

    public List<ComboBoxModel> getType(){
        List<ComboBoxModel>  list = new ArrayList<>();
        for (AcceptanceEnum acceptanceEnum:AcceptanceEnum.values()) {
            ComboBoxModel comboBoxModel = new ComboBoxModel();
            comboBoxModel.setId(acceptanceEnum.getType());
            comboBoxModel.setName(acceptanceEnum.getDesc());
            list.add(comboBoxModel);
        }
        return list;
    }


    public ResultPage<AcceptanceLimit>  getPage(Integer page,Integer limit){

        Pageable pageable = PageRequest.of(page-1,limit);
        Page<AcceptanceLimit> all = this.acceptanceLimitRepository.findAll(pageable);
        return new ResultPage(all.getContent(),all.getNumber(),all.getSize(),all.getTotalElements());
    }


    public AcceptanceLimit  add(AcceptanceLimit acceptance){
        AcceptanceLimit acceptanceLimit = this.acceptanceLimitRepository.queryByTeilAndMerkmal(acceptance.getTeil(),acceptance.getMerkmal());
        if(acceptanceLimit == null){
            acceptanceLimit = acceptance;
        }else{
            acceptanceLimit.setType(acceptance.getType());
            acceptanceLimit.setDesc(acceptance.getDesc());
            acceptanceLimit.setValue(acceptance.getValue());
        }
        return this.acceptanceLimitRepository.save(acceptanceLimit);
    }


    public AcceptanceLimit  updateStatus(String id,Integer status) throws CustomException {
        AcceptanceLimit acceptanceLimit = this.acceptanceLimitRepository.findById(id).orElse(null);
        if(acceptanceLimit == null){
            throw new CustomException("数据不存在");
        }
        //启动
        if(status == 1 ){
            AcceptanceLimit acceptanceLimitByStatus = this.acceptanceLimitRepository.getAcceptanceLimitByStatus();
            if(acceptanceLimitByStatus != null && !acceptanceLimitByStatus.getId().equals(acceptanceLimit.getId())){
                throw new CustomException("请关闭已启动的数据！");
            }else{
                acceptanceLimit.setStatus(status);
            }
        }
        return this.acceptanceLimitRepository.save(acceptanceLimit);
    }


    public void delete(String id){
        this.acceptanceLimitRepository.deleteById(id);
    }
}
